﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    public class PageCommon_Carousel:ControlBase
    {
        private IEnumerable<SelectorItem> _urls = null;

        public PageCommon_Carousel(ViewContext viewContext) : base(viewContext)
        {
            this.Css("carousel slide").Data("ride", "carousel");

        }

        public PageCommon_Carousel ImageUrls(IEnumerable<string> urls)
        {
            _urls = urls.Select(x=>new SelectorItem("",x));
            return this;
        }

        /// <summary>
        /// 设置选择的显示项目,,title=显示的标题  value=图片地址
        /// </summary>
        /// <param name="urls"></param>
        /// <returns></returns>
        public PageCommon_Carousel ImageUrls(IEnumerable<SelectorItem> urls)
        {
            _urls = urls;
            return this;
        }

        /// <summary>
        /// 设置选择的显示项目,,title=显示的标题  key=图片地址
        /// </summary>
        /// <param name="urls"></param>
        /// <returns></returns>
        public PageCommon_Carousel ImageUrls<TData>(IEnumerable<TData> sources,Func<TData,string> titleGetter,Func<TData,string> urlGetter)
        {
            _urls = sources.Select(x=>new SelectorItem(titleGetter(x),urlGetter(x)));

            return this;
        }

        public override HtmlString Render()
        {
            var lst = _urls.ToArrayEx();

            var id = Attributes.GetValue("id", "");

            if (string.IsNullOrWhiteSpace(id))
            {
                id = "carousel_" + AdminLTEUI.NextControlIndex();
                Attributes.ID(id);
            }

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            ViewContext.Writer.WriteLine("<ol class=\"carousel-indicators\">");

            for (int i = 0; i < lst.Length; i++)
            {
                ViewContext.Writer.WriteLine($"<li data-target=\"#{id}\" data-slide-to=\"{i}\" class=\"{(i==0).If("active")}\"></li>");
            }

            ViewContext.Writer.WriteLine("</ol>");

            ViewContext.Writer.WriteLine("<div class=\"carousel-inner\">");

            for (int i = 0; i < lst.Length; i++)
            {
                var item = lst[i];

                ViewContext.Writer.WriteLine($"<div class=\"item {(i==0).If("active")}\">");

                ViewContext.Writer.WriteLine($"<img src=\"{item.Value}\" alt=\"{item.Title}\">");

                ViewContext.Writer.WriteLine($"<div class=\"carousel-caption\">{item.Title}</div>");

                ViewContext.Writer.WriteLine("</div>");
            }

            ViewContext.Writer.WriteLine("</div>");


            ViewContext.Writer.WriteLine("</div> <!-- .carousel -->");

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }

            return HtmlString.Empty;
        }
    }
}
